﻿
using WanPiJie;
using System;
using Collection.Core;
using Collection.Entities;
using System.Collections.Generic;
using WanPiJie.Products;
using System.Collections.Specialized;
using System.Configuration;
using Collection.Core.Goods;
namespace Collection.Core.Processing
{
	/// <summary>
	/// 商品导出类
	/// </summary>
	public class Exporter
	{

		/// <summary>
		/// 商品所属单品匹配器
		/// </summary>
		private static IDictionary<CatalogEnumerate, BaseGoods> matcher;

		/// <summary>
		/// 商品单品匹配器
		/// </summary>
		public static IDictionary<CatalogEnumerate, BaseGoods> Matcher
		{
			get
			{
				return matcher;
			}
		}

		/// <summary>
		/// 静态构造函数
		/// </summary>
		static Exporter()
		{
			matcher = new Dictionary<CatalogEnumerate, BaseGoods> { 
				{CatalogEnumerate.Milk,new Milk()}
				,{CatalogEnumerate.Diapers,new Diapers()}
				,{CatalogEnumerate.MilkBottle,new MilkBottle()}
				,{CatalogEnumerate.Nipple,new Nipple()}
			};
		}


		#region 商品属性信息的导出
		/// <summary>
		///  导入属性数据信息至顽皮街库
		/// </summary>
		/// <param name="attributesOversee">属性导入回调</param>
		/// <param name="attributeValuesOversee">属性值导入回调</param>
		public static void ExportProperties(Action<ProductAttribute, string> attributesOversee, Action<ProductAttributeValue, string> attributeValuesOversee)
		{
			var catalogs = ServiceFactory.CatalogService.GetCatalogs();
			IList<Property> properties;
			IList<PropertyValue> propertyValues;
			foreach (var catalog in catalogs)
			{
				properties = ServiceFactory.CatalogService.GetPropertiesByCatalog(catalog.Id);
				foreach (var property in properties)
				{
					CategoryService.Instance.Import(property.ToProductAttribute(), attributesOversee);
					propertyValues = ServiceFactory.CatalogService.GetPropertyValues(catalog.Id, property.Id);
					foreach (var propertyValue in propertyValues)
					{
						CategoryService.Instance.Import(propertyValue.ToProductAttributeValue(), attributeValuesOversee);
					}
				}
			}
		}
		#endregion

		#region 商品销售信息的导出
		/// <summary>
		/// 导出商品数据至顽皮街数据库（MongoDB）
		/// </summary>
		/// <param name="oversee">回调监控</param>
		/// <param name="pageSize">每次导入的行数</param>
		public static void Exporting(Action<SalesInfoExtension, string> oversee)
		{
			Int32 pageSize = 50;
			Int64 rowCount = ServiceFactory.MerchandiseService.Count;
			Int64 pageCount = rowCount / pageSize + ((rowCount % pageSize) > 0 ? 1 : 0);
			List<Merchandise> products;
			for (Int32 i = 1; i <= pageCount; i++)
			{
				products = ServiceFactory.MerchandiseService.GetMerchandises(i, pageSize);
				foreach (var product in products)
				{
					Exporting(product, oversee);
				}
			}
		}

		/// <summary>
		/// 导出商品数据至顽皮街数据库
		/// </summary>
		/// <param name="Oversee">回调监控</param>
		/// <param name="merchandise">商品信息</param>
		public static void Exporting(Merchandise merchandise, Action<SalesInfoExtension, string> oversee)
		{
			SalesInfoExtension salesInfoExtension = merchandise.ToSalesInfoExtension();
			salesInfoExtension.InitializeAttributes(Parser.DecomposeProperties(merchandise.Properties));
			try
			{
				var product = Matcher[(CatalogEnumerate)merchandise.CatalogId].Matcher(merchandise.Properties);
				if (product != null)
				{
					salesInfoExtension.SingleProductId = product.SerialNumber;
				}
				else
				{
					salesInfoExtension.SingleProductId =0;
				}
			}
			catch (NullReferenceException)
			{
				salesInfoExtension.SingleProductId = 0;
			}
			catch (KeyNotFoundException)
			{
				salesInfoExtension.SingleProductId = 0;
			}
			salesInfoExtension.CatalogId = WpjSf.CategoryService.GetCatalogByThirdId(salesInfoExtension.CatalogId).Id;

			//导出至mongo数据库
			WpjSf.ProductSalesInfoService.Import(salesInfoExtension, oversee);
		}
		#endregion
	}
}